#include "libraries.h"

using namespace std;

Centipede::Centipede()
{
    quit = false;
    defaultFont = NULL;
    /*Start SDL.  If it fails, then quit.*/
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1) quit = true;
    if( TTF_Init() == -1) quit = true;
    if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1) quit = true;
    sounds.loadSounds(); //Load sounds for the game
    gameState = 1;
    choice = 1;
    /* Set up screen and screen caption */
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE );
    if( screen == NULL) quit = true; /* If screen didn't work, then quit */
    SDL_WM_SetCaption( "Centipedic - ",NULL );
    defaultFont = TTF_OpenFont("Images/freesansbold.ttf", 11);
    srand(time(NULL)); /* Initialize random seed */
    /* Frames should start at 0 */
    frame = 0; /* frame is used for FPS as well as passing as an argument to createEnemiesQueue() */
    /* Start timers */
    update.start();
    fps.start();
    fpsCap.start();
    menuDelay.start();
    /*Load starter image*/
    imgLoad(background, "Images/Backgrounds/menu.png");
    //background = IMG_Load("Images/Backgrounds/bossStarLevel2.png");
    playerList.pushBack(Player(400, 500, "Images/Ship/shipCenterNew2.png"));
    topScores = new highScore();
}

Centipede::~Centipede()
{
    SDL_FreeSurface( background );
    SDL_FreeSurface( screen );
    SDL_FreeSurface( select );
    if(defaultFont) TTF_CloseFont(defaultFont);
    Mix_CloseAudio();
    TTF_Quit();
    SDL_Quit();
}

/* This is where the game "starts" and can be considered the main loop */
void Centipede::play()
{
    while( !quit)
    {
        if( gameState == 1) /*Main Menu*/
        {
            textList.erase();
            menuDelay.start(); //Restart menuDelay timer from other gameStates
            imgLoad(background, "Images/Backgrounds/menu.png");
            imgLoadAlpha(select, "Images/User Interface/select.png");
            Mix_PlayMusic( sounds.getMainMenuMusic(), -1); //Play music for main menu
            level.setLevel(1);
            playerList[0].setLives(5);
            playerList[0].setPoints(0);
            //background = IMG_Load("Images/Backgrounds/bossStarLevel2.png");
            while( gameState == 1 )
            {
                SDL_PollEvent( &event );
                SDL_PumpEvents(); /*Without this, bad stuff happens*/
                screenDraw(); /*Update the screen*/
                /*Play sounds*/
                keyPressed( SDL_GetKeyState(NULL) );/*getKeyPressed*/
                SDL_Flip( screen );
                speedCheck();
                endCheck();
            }
        }

        else if( gameState == 2) /*Second Menu*/
        {
            textList.erase();
            imgLoad(background, "Images/Backgrounds/enterName.png");
            while( gameState == 2)
            {
                SDL_PollEvent( &event );
                SDL_PumpEvents(); /*Without this, bad stuff happens*/
                keyPressed( SDL_GetKeyState(NULL) );/*getKeyPressed*/
                screenDraw(); /*Update the screen*/
                if(textList.getSize() > 0)
                {
                	textList.erase();
                }
                textList.pushBack(TextElement(sNameInput, 441, 207, 255, 255, 255, 30, "center", "freesansbold.tff"));

                /*Play sounds*/
                SDL_Flip( screen );
                speedCheck();
                endCheck();
            }
        }

        else if( gameState == 3) /*Classic Mode*/
        {
            textList.erase();
            centipedeList.erase();
            enemyList.erase();
            imgLoad(background, "Images/Backgrounds/bossStarLevel2.png");
            level.levelSetup(playerList, enemyList, centipedeList, frame);
            while( gameState == 3)
            {
                for(int i=0; i < ACCURACY_CHECK; i++)
                {
                    SDL_PollEvent( &event );
                    SDL_PumpEvents(); /*Without this, bad stuff happens*/
                    /*Play sounds*/
                    keyPressed( SDL_GetKeyState(NULL) );/*getKeyPressed*/
                    collisionOccur();
                }
                level.levelUpdate(playerList, enemyList, centipedeList, frame, gameState);
                screenDraw(); /*Update the screen*/
                SDL_Flip( screen );
                speedCheck();
                endCheck();
            }
        }

        else if( gameState == 4) /*Hi Scores*/
        {
            textList.erase();
            imgLoad(background, "Images/Backgrounds/highScore.png");
            topScores->readFile();
            while( gameState == 4)
            {
                SDL_PollEvent( &event );
                SDL_PumpEvents(); /*Without this, bad stuff happens*/
                /*Play sounds*/
                keyPressed( SDL_GetKeyState(NULL) );/*getKeyPressed*/
                screenDraw(); /*Update the screen*/
                SDL_Flip( screen );
                speedCheck();
                endCheck();
            }
        }
        else if( gameState == 5) /*Hi Scores at Game Over*/
        {
            textList.erase();
            imgLoad(background, "Images/Backgrounds/highScore.png");
            topScores->updateScore((playerList[0]).getPoints(), sNameInput);
            topScores->updateFile();
            topScores->readFile();
            menuDelay.start();
            while( gameState == 5 && menuDelay.get_ticks() < 6000)
            {
                SDL_PollEvent( &event );
                SDL_PumpEvents(); /*Without this, bad stuff happens*/
                /*Play sounds*/
                keyPressed( SDL_GetKeyState(NULL) );/*getKeyPressed*/
                screenDraw(); /*Update the screen*/
                SDL_Flip( screen );
                speedCheck();
                endCheck();
            }
            gameState = 6;
        }
        else if( gameState == 6) /*Game Over*/
		{
		    textList.erase();
			imgLoad(background, "Images/Backgrounds/gameover.png");
			menuDelay.start();
			while( gameState == 6 && menuDelay.get_ticks() < 6000)
			{
				SDL_PollEvent( &event );
				SDL_PumpEvents(); /*Without this, bad stuff happens*/
				/*Play sounds*/
				keyPressed( SDL_GetKeyState(NULL) );/*getKeyPressed*/
				screenDraw(); /*Update the screen*/
				SDL_Flip( screen );
				speedCheck();
				endCheck();
			}

			gameState = 1;
		}
    }
}

void Centipede::endCheck()
{
    if(event.type == SDL_QUIT)
    {
        quit = true;
    }
    if(quit) gameState = -1; /*This sentinel ends the game nicely*/
}

//For screenDraw, pList is the list of players, eList is list of enemies,
//aList is list of ammo.
void Centipede::screenDraw()
{
    SDL_BlitSurface( background, NULL, screen, NULL );
    if(gameState == 1)
    {
        if(choice == 1)
        {
            apply_surface(442, 92, select, screen);
        }
        else if(choice == 2)
        {
            apply_surface(442, 240, select, screen);
        }
        else if(choice == 3)
        {
            apply_surface(442, 400, select, screen);
        }
    }
    else if( gameState == 2 )
    {
        layerDraw(textList);
    }
    else if( gameState == 3 )
    {
        layerDraw(playerList);
        for(int i=0; i < playerList.getSize(); i++)
        {
            layerDraw(playerList[i].getAmmoList());
        }
        layerDraw(enemyList);
        for(int i=0; i < centipedeList.getSize(); i++)
        {
            layerDraw(centipedeList[i].getCentipedeParts());
        }
        layerDraw(textList);
    }
    else if( gameState == 4 )
    {
    	topScores->printScreen(screen);
    }
    else if( gameState == 5 )
    {
    	topScores->printScreen(screen);
    }
    frame++; /* Every time the screen is drawn, increase frame by 1 */
}

/*layerDraw is ONLY called by screenDraw!!!*/
template<class T>
void Centipede::layerDraw(list<T>& layer)
{
    int counter=0;
    int lSize = layer.getSize();
    while(counter < lSize)
    {
        layer[counter].draw(screen);
        counter++;
    }
}

void Centipede::speedCheck()
{
    /*Show FPS if in-game */
    if( gameState && update.get_ticks() > 1000 )
    {
        stringstream fpsDisplay;
        if(gameState == 1 || gameState == 2 || gameState == 4)
        {
            fpsDisplay << "Centipedic - FPS: " << frame / (fps.get_ticks()/1000) << " " << playerList[0].getPoints();
        }
        else if(gameState == 3)
        {
            if(playerList.getSize() > 0)
            {
                /* Ghetto score and lives display */
                fpsDisplay << "Centipedic - FPS: " << frame / (fps.get_ticks()/1000) << " | Score: " << playerList[0].getPoints() << " | Lives: " << playerList[0].getLives() << " | Level: " << level.getCurrentLevel();
            }
            else fpsDisplay << "Centipedic - FPS: " << frame / (fps.get_ticks()/1000) << " " << playerList[0].getPoints();
        }
        SDL_WM_SetCaption( fpsDisplay.str().c_str(), NULL);
        update.start(); /* Restart update from 0 */
    }

    /* Cap FPS if too high */
    if( fpsCap.get_ticks() < 1000/FRAMES_PER_SECOND)
    {
        SDL_Delay(( 1000/FRAMES_PER_SECOND ) - fpsCap.get_ticks() );
    }
    fpsCap.start();
}

void Centipede::keyPressed(Uint8* keyinput)
{
    int x=0, y=0; /* Used for mouse offsets.  Prevents eronious behavior. */
    if(keyinput[SDLK_ESCAPE] || ((keyinput[SDLK_LALT] || keyinput[SDLK_RALT]) && keyinput[SDLK_F4]))
    {
        quit = true;
    }
    /* Main Menu */
    if(gameState == 1)
    {
        bool choiceSelection = false; /* Used to determine if mouse is over boundaries for a menu choice */

        /* Keyboard input */
        if(keyinput[SDLK_DOWN] && !keyinput[SDLK_UP] && menuDelay.get_ticks() > 300)
        {
            /* Move cursor down or reset to top if already at bottom */
            if(choice < 3) choice++;
            else choice = 1;
            Mix_PlayChannel(-1, sounds.getShoock(), 0);
            menuDelay.start(); /* Restart the timer */
        }
        else if(keyinput[SDLK_UP] && !keyinput[SDLK_DOWN] && menuDelay.get_ticks() > 300)
        {
            /* Move cursor up or reset to bottom if already at top */
            if(choice > 1) choice--;
            else choice = 3;
            Mix_PlayChannel(-1, sounds.getShoock(), 0);
            menuDelay.start(); /* Restart the timer */
        }

        /* Mouse input */
        if(event.type == SDL_MOUSEMOTION || event.type == SDL_MOUSEBUTTONDOWN)
        {
            x = event.motion.x;
            y = event.motion.y;
            int checkChoice=0;
            choiceSelection = true;
            /* Menu selections all have the same width and x-coord*/
            /* 295 is the width of the select.  123 is the height of the select. */
            if(x >= 442 && x <= 442 + 295)
            {
                if     (y >= 92  && y <= 92  + 123) checkChoice = 1;
                else if(y >= 240 && y <= 240 + 123) checkChoice = 2;
                else if(y >= 400 && y <= 400 + 123) checkChoice = 3;
                else choiceSelection = false;
                if(checkChoice != 0 && checkChoice != choice)
                {
                    choice = checkChoice;
                    Mix_PlayChannel(-1, sounds.getShoock(), 0);
                }
            }
            else choiceSelection = false;
            if(choiceSelection) /* Checks to see if mouse is pressed, and goes to selection check if true */
            {
                choiceSelection = false;
                if(event.type == SDL_MOUSEBUTTONDOWN) choiceSelection = true;
            }
        }

        /* Selection Check */
        if(keyinput[SDLK_KP_ENTER] || keyinput[SDLK_RETURN] || choiceSelection)
        {
            if(choice == 1)
            {
                gameState = 2;
            }
            else if(choice == 2)
            {
                gameState = 4;
            }
            else if(choice == 3)
            {
                quit = true;
            }
        }
    }
    else if( gameState == 2 )
	{
    	//Delays input so a single character will be read.
		if(menuDelay.get_ticks() > 150)
		{
			if(sNameInput.size() < 4)
			{
				if(keyinput[SDLK_0])
					sNameInput += '0';
				else if(keyinput[SDLK_1])
					sNameInput += '1';
				else if(keyinput[SDLK_2])
					sNameInput += '2';
				else if(keyinput[SDLK_3])
					sNameInput += '3';
				else if(keyinput[SDLK_4])
					sNameInput += '4';
				else if(keyinput[SDLK_5])
					sNameInput += '5';
				else if(keyinput[SDLK_6])
					sNameInput += '6';
				else if(keyinput[SDLK_7])
					sNameInput += '7';
				else if(keyinput[SDLK_8])
					sNameInput += '8';
				else if(keyinput[SDLK_9])
					sNameInput += '9';


				else if(keyinput[SDLK_a] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'a';
				else if(keyinput[SDLK_b] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'b';
				else if(keyinput[SDLK_c] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'c';
				else if(keyinput[SDLK_d] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'd';
				else if(keyinput[SDLK_e] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'e';
				else if(keyinput[SDLK_f] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'f';
				else if(keyinput[SDLK_g] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'g';
				else if(keyinput[SDLK_h] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'h';
				else if(keyinput[SDLK_i] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'i';
				else if(keyinput[SDLK_j] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'j';
				else if(keyinput[SDLK_k] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'k';
				else if(keyinput[SDLK_l] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'l';
				else if(keyinput[SDLK_m] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'm';
				else if(keyinput[SDLK_n] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'n';
				else if(keyinput[SDLK_o] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'o';
				else if(keyinput[SDLK_p] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'p';
				else if(keyinput[SDLK_q] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'q';
				else if(keyinput[SDLK_r] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'r';
				else if(keyinput[SDLK_s] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 's';
				else if(keyinput[SDLK_t] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 't';
				else if(keyinput[SDLK_u] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'u';
				else if(keyinput[SDLK_v] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'v';
				else if(keyinput[SDLK_w] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'w';
				else if(keyinput[SDLK_x] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'x';
				else if(keyinput[SDLK_y] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'y';
				else if(keyinput[SDLK_z] && !(keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'z';

				else if(keyinput[SDLK_a] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'A';
				else if(keyinput[SDLK_b] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'B';
				else if(keyinput[SDLK_c] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'C';
				else if(keyinput[SDLK_d] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'D';
				else if(keyinput[SDLK_e] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'E';
				else if(keyinput[SDLK_f] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'F';
				else if(keyinput[SDLK_g] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'G';
				else if(keyinput[SDLK_h] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'H';
				else if(keyinput[SDLK_i] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'I';
				else if(keyinput[SDLK_j] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'J';
				else if(keyinput[SDLK_k] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'K';
				else if(keyinput[SDLK_l] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'L';
				else if(keyinput[SDLK_m] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'M';
				else if(keyinput[SDLK_n] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'N';
				else if(keyinput[SDLK_o] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'O';
				else if(keyinput[SDLK_p] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'P';
				else if(keyinput[SDLK_q] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'Q';
				else if(keyinput[SDLK_r] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'R';
				else if(keyinput[SDLK_s] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'S';
				else if(keyinput[SDLK_t] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'T';
				else if(keyinput[SDLK_u] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'U';
				else if(keyinput[SDLK_v] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'V';
				else if(keyinput[SDLK_w] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'W';
				else if(keyinput[SDLK_x] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'X';
				else if(keyinput[SDLK_y] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'Y';
				else if(keyinput[SDLK_z] && (keyinput[SDLK_RSHIFT] || keyinput[SDLK_LSHIFT]))
					sNameInput += 'Z';
			}
			if((keyinput[SDLK_DELETE] || keyinput[SDLK_BACKSPACE]) && sNameInput.size() > 0)
			{
				sNameInput.erase((sNameInput.size()-1));
			}

			if(keyinput[SDLK_KP_ENTER] || keyinput[SDLK_RETURN])
				{
					textList.erase();
					gameState = 3;
				}

			menuDelay.start();
		}

	}
    /*Classic Mode*/
    else if( gameState == 3 )
    {
        /*Move all players and player ammo*/
        for(int i=0; i < playerList.getSize(); i++)
        {
            playerList[i].move(keyinput);
        }

        /*Move all enemies*/
        for(int i=0; i < enemyList.getSize(); i++)
        {
            i += enemyList[i].move(enemyList, i, frame); /*enemyList returns changes in index value*/
        }
        for(int i=0; i < centipedeList.getSize(); i++)
        {
            centipedeList[i].move(enemyList);
        }
    }

    else if( gameState == 4 )
    {
    	if(menuDelay.get_ticks() > 150)
    	{
    		if(keyinput[SDLK_SPACE])
    		{
    			gameState = 1;
    		}
    	}
    }
}

void Centipede::collisionOccur()
{
    double basicShot = 1.0; /* Damage modifier for type of ammo */
    for(int i=0; i < playerList.getSize(); i++)
    {
        for(int j=0; j < playerList[i].ammunition.getSize(); j++)
        {
            if(playerList[i].ammunition[j].getIdNumber() == 1) /* basicShot */
            {
                for(int k=0; k < enemyList.getSize(); k++)
                {
                    if(enemyList[k].getIdNumber() >= 1) /* If enemy exists */
                    {
                        /* If there has been a collision and the enemy has more than 0 hp*/
                        if(collideRect(enemyList[k].getPos(), playerList[i].ammunition[j].getPos()) && enemyList[k].getHealth() > 0)
                        {
                            playerList[i].setPoints(playerList[i].getPoints() + int((1.0 * basicShot) * (1.0 * enemyList[k].getValue() / enemyList[k].getHealth())));
                            enemyList[k].setValue(  enemyList[k].getValue()   - int(   (1.0 * basicShot) * (1.0 * enemyList[k].getValue() / enemyList[k].getHealth())));
                            enemyList[k].setHealth( enemyList[k].getHealth()  - int(   (1.0 * basicShot)));

                            /*Delay deletion of ammo when it collides to preserve for-loop location*/
                            playerList[i].ammunition[j].setIdNumber(0);
                            playerList[i].ammunition[j].setX(SCREEN_WIDTH+300);
                            playerList[i].ammunition[j].setY(SCREEN_HEIGHT+300);
                            playerList[i].ammunition[j].updatePos();
                        }
                    }
                }
                for(int k=0; k < centipedeList.getSize(); k++)
                {
                    for(int l=0; l < centipedeList[k].centipedeParts.getSize(); l++)
                    {
                        if(collideRect(centipedeList[k].centipedeParts[l].getPos(), playerList[i].ammunition[j].getPos()) && centipedeList[k].centipedeParts[l].getHealth() > 0)
                        {
                            playerList[i].setPoints(playerList[i].getPoints() + int((1.0 * basicShot) * (1.0 * centipedeList[k].centipedeParts[l].getValue() / centipedeList[k].centipedeParts[l].getHealth())));
                            centipedeList[k].centipedeParts[l].setValue( centipedeList[k].centipedeParts[l].getValue() - int( (1.0 * basicShot) * (1.0 * centipedeList[k].centipedeParts[l].getValue() / centipedeList[k].centipedeParts[l].getHealth())));
                            centipedeList[k].centipedeParts[l].setHealth( centipedeList[k].centipedeParts[l].getHealth() - int( (1.0 * basicShot)));

                            /*Delay deletion of ammo when it collides to preserve for-loop location*/
                            playerList[i].ammunition[j].setIdNumber(0);
                            playerList[i].ammunition[j].setX(SCREEN_WIDTH+300);
                            playerList[i].ammunition[j].setY(SCREEN_HEIGHT+300);
                            playerList[i].ammunition[j].updatePos();
                        }
                    }
                }
            }
        }
        for(int j=0; j < enemyList.getSize(); j++)
        {
            if(enemyList[j].getIdNumber() > 0 && collideRect(enemyList[j].getPos(), playerList[i].getPos()))
            {
                playerList[i].setIsDead(true);
                enemyList[j].setHealth( enemyList[j].getHealth() - enemyList[j].getSelfDamage());
            }
            if(enemyList[j].getIdNumber() && enemyList[j].getHealth() <= 0)
            {
                playerList[i].setPoints( playerList[i].getPoints() + enemyList[j].getValue());
                enemyList.popElement(j);
                j--;
            }
        }
        for(int j=0; j < centipedeList.getSize(); j++)
        {
            bool killYourself=false;
            for(int k=0; !killYourself && k < centipedeList[j].centipedeParts.getSize(); k++)
            {
                if(centipedeList[j].centipedeParts[k].getIdNumber() > 0 && collideRect(centipedeList[j].centipedeParts[k].getPos(), playerList[i].getPos()))
                {
                    playerList[i].setIsDead(true);
                    centipedeList[j].centipedeParts[k].setHealth( centipedeList[j].centipedeParts[k].getHealth() - centipedeList[j].centipedeParts[k].getSelfDamage());
                }
                if(centipedeList[j].centipedeParts[k].getIdNumber() && centipedeList[j].centipedeParts[k].getHealth() <= 0)
                {
                    centipedeList[j].collision(centipedeList, k);
                    k = centipedeList[j].centipedeParts.getSize(); //Needed to prevent possible crash
                    killYourself=true;
                }
            }
            if(killYourself)
            {
                centipedeList.popElement(j);
                j--;
            }
        }
    }
}
